import Vue from 'vue';
import Router from 'vue-router';
import Cookies from 'js-cookie'; 
import behind from './behindRoute.js';
import front from './frontRoute.js';
import { refreshToken, authLogin } from '../api/index.js';

Vue.use(Router);

// 跳转同路由不报错处理
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}

// 合并后台系统、移动端路由表
behind.push(...front);

let router = new Router({ mode: 'history', routes: behind });

// 进入页面前验证
router.beforeEach((to, from, next) => {
	let that = router.app;
	document.title = `知了 | ${to.meta.title}`;		

	if (to.path.search('exampreview') != -1){
		next();
	}

	if (!to.meta.permission && !to.meta.device) { // 验证PC端token是否过期
		next();	
		// const refreshtoken = Cookies.get('refreshtoken') ? Cookies.get('refreshtoken') : "";
		// refreshToken({ refreshtoken, username: localStorage.getItem('ms_username'), shopuuid: localStorage.getItem('shopuuid') }).then((res) => {
		// 	// console.log('refresh:',res);
		// 	if (res.code == 1) {
		// 		Cookies.set('token', res.data.token);
		// 		next();		
		// 	} else if (res.code == 2) {
		// 		next();
			// } else {
			// 	next('/login');	
			// }
		// })
	}	
	
	if (to.meta.verify) {	// 验证前端token是否过期	   
		// const frontrefreshtoken = Cookies.get('frontrefreshtoken') ? Cookies.get('frontrefreshtoken') || localStorage.getItem('frontrefreshtoken') : "";
		// that.$frontaxios.post(that.$front.frontRefreshToken, { frontrefreshtoken, phone: localStorage.getItem('phone') }).then((res) => {
		// 	if (res.code == 1) {
		// 		Cookies.set('fronttoken', res.data.fronttoken);
		// 		localStorage.setItem('fronttoken', res.data.token);
		// 		next();
		// 	} else if (res.code == 2) {
		// 		next();
		// 	} else if (res.code == -10) {
		// 		return that.$dialog.alert({ title: "温馨提示", message: "店铺使用期限已到，请购买后再来查看" }); 
		// 	} else {				
		// 		next();
		// 	}
		// })
	}

	if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
		that.$confirm('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
			confirmButtonText: '确定'
		});
	} else {
		next();
	}
});

// 进了页面以后验证
router.afterEach((to, from) => {
	let that = router.app;		
	if (isWeixin()) {	// 前端在微信开发者工具访问验证用户是否登录
		let logintag = sessionStorage.getItem('logintag') ? sessionStorage.getItem('logintag') : 0;
		if(to.path.search('exampreview') != -1){
			next();
		}		
		if(to.path.search('downfiles') != -1){
			next();
		}
		if (to.meta.verify && logintag == 0) {			
			return that.$router.push({ name: 'my',query: {'domainmame': to.params.domainmame } });
		}		
	} else {
		if (to.meta.device) {	// 前端在浏览器访问验证用户是否登录
			// let tags = sessionStorage.getItem('htmllogintag') ? sessionStorage.getItem('htmllogintag') : 0;
			// if (to.meta.verify && tags == 0) {			
			// 	return that.$router.push({ name: 'my',query: {'domainmame': to.params.domainmame } });
			// }
		}
	}	

	if (to.path.search('/shop') != -1 || to.path.search('/xuejing') != -1) { // 前端在浏览器访问验证用户是否登录		
		if (that.$route.params.domainname) {
			Cookies.set('frontdomainname', that.$route.params.domainname);
			localStorage.setItem('domainname', that.$route.params.domainname);
		}
		let shop_id = localStorage.getItem('shopuuid')?localStorage.getItem('shopuuid'):''
		that.$frontaxios.post(that.$front.getShopName,{shop_id}).then((res) => {
			if (res.code == 1) {
				localStorage.setItem('shopname', res.data.shopname)
			} else {
				console.log('获取店铺名失败');
			}
		});
	}
})

export default router;
